package com.example.mall.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.mall.constants.BaseConstants;
import com.example.mall.domain.PaymentMethods;
import com.example.mall.dto.PaymentMethodsDto;
import com.example.mall.mapper.PaymentMethodsMapper;
import com.example.mall.query.PageDTO;
import com.example.mall.query.PaymentMethodsQuery;
import com.example.mall.service.IPaymentMethodsService;

import com.example.mall.util.AssertUtil;
import com.example.mall.util.CodeGenerateUtils;
import com.example.mall.util.CopyUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * <p>
 * 支付方式表 服务实现类
 * </p>
 *
 * @author whale
 * @since 2025-05-09
 */
@Service
public class PaymentMethodsServiceImpl extends ServiceImpl<PaymentMethodsMapper, PaymentMethods> implements IPaymentMethodsService {

    @Autowired
    private PaymentMethodsMapper paymentMethodsMapper;

    @Override
    public PageDTO<PaymentMethodsDto> queryPage(PaymentMethodsQuery paymentMethodsQuery) {
        if (paymentMethodsQuery == null) {
            paymentMethodsQuery = new PaymentMethodsQuery();
        }
        Page<PaymentMethods> page = paymentMethodsQuery.toMpPageByDefaultCreateTimeAsc();
        Page<PaymentMethods> cartPage = lambdaQuery()
                .like(paymentMethodsQuery.getMethodId()!= null, PaymentMethods::getMethodId, paymentMethodsQuery.getMethodId())
                .like(paymentMethodsQuery.getDescription()!= null, PaymentMethods::getDescription, paymentMethodsQuery.getDescription())
                .like(paymentMethodsQuery.getIsActive()!= null, PaymentMethods::getIsActive, paymentMethodsQuery.getIsActive())
                .like(paymentMethodsQuery.getName()!= null, PaymentMethods::getName, paymentMethodsQuery.getName())
                .like(paymentMethodsQuery.getPlatformId()!= null, PaymentMethods::getPlatformId, paymentMethodsQuery.getPlatformId())
                .orderByDesc(PaymentMethods::getUpdateDate)
                .page(page);
        return PageDTO.of(cartPage, PaymentMethodsDto.class);
    }

    @Override
    public PaymentMethods queryPaymentMethodsById(Long methodId) {
        AssertUtil.isNotNull(methodId, "methodId不能为空");
        return paymentMethodsMapper.selectById(methodId);
    }

    @Override
    public List<PaymentMethods> queryPaymentMethodsList() {
        return this.list();
    }

    @Override
    public void insert(PaymentMethodsDto paymentMethodsDto) {
        PaymentMethods paymentMethods = new PaymentMethods();
        CopyUtils.copyProperties(paymentMethodsDto, paymentMethods);
        paymentMethods.setMethodId(CodeGenerateUtils.generateUniqueId());
        paymentMethods.setIsActive(BaseConstants.EFFECTIVE_STATUS);
        paymentMethods.setCreateDate(new Date());
        paymentMethods.setUpdateDate(new Date());
        paymentMethodsMapper.insert(paymentMethods);
    }

    @Override
    public void update(PaymentMethodsDto paymentMethodsDto) {
        AssertUtil.isNotNull(paymentMethodsDto.getMethodId(), "methodId不能为空");
        PaymentMethods paymentMethods = paymentMethodsMapper.selectById(paymentMethodsDto.getMethodId());
        AssertUtil.isNotNull(paymentMethods, "支付方式不存在");
        CopyUtils.copyProperties(paymentMethodsDto, paymentMethods);
        paymentMethods.setUpdateDate(new Date());
        paymentMethodsMapper.updateById(paymentMethods);
    }

    @Override
    public void delete(Long methodId) {
        AssertUtil.isNotNull(methodId, "methodId不能为空");
        PaymentMethods paymentMethods = paymentMethodsMapper.selectById(methodId);
        AssertUtil.isNotNull(paymentMethods, "支付方式不存在");
        paymentMethods.setIsActive(BaseConstants.LOSE_STATUS);
        paymentMethods.setUpdateDate(new Date());
        paymentMethodsMapper.updateById(paymentMethods);
    }

    @Override
    public void deleteBatch(List<Long> methodIds) {
        AssertUtil.isNotNull(methodIds, "methodIds不能为空");
        List<PaymentMethods> paymentMethods = paymentMethodsMapper.selectBatchIds(methodIds);
        AssertUtil.isNotNull(paymentMethods, "支付方式不存在");
        for (PaymentMethods paymentMethod : paymentMethods) {
            paymentMethod.setIsActive(BaseConstants.LOSE_STATUS);
            paymentMethod.setUpdateDate(new Date());
            paymentMethodsMapper.updateById(paymentMethod);
        }
    }
}
